/*
* Entourage
* Copyright 2011 INF532@Galatasaray University & Vincent Labatut 
* 
* This file is part of Entourage.
* 
* Entourage is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Entourage is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with Entourage. If not, see <http://www.gnu.org/licenses/>.
* 
*/
package com.appspot.entourage.webpageparser;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.appspot.entourage.common.NamedLink;
import com.appspot.entourage.common.Person;



/**
 * This class is used to extract links and expressions with
 * the method getNamedLinks and find the title of a personality 
 * with the method getNamedLink
 * @author Zakaria BOUHAMIDI <zakariabouhamidi@hotmail.com>
 * @version 1.0
 * 
 *
 */

public class WebPageParser {
	
	/**
	 * This  method Search expressions and urls
	 *
	 * @param url url of one page
	 * @return a list of words and urls
	 * @author Zakaria BOUHAMIDI <zakariabouhamidi@hotmail.com>
	 */
	
	 public static   List<NamedLink> getNamedLinks(String url)
		
	 {	List<NamedLink> list = new ArrayList<NamedLink>();
	 	Document doc = null;
	 	try 
     	 {
		      doc = Jsoup.connect(url).get();
         }
     	catch (IOException e) 
     	{
     		// TODO Auto-generated catch block
     		e.printStackTrace();
	     }
        
         Elements links = doc.select("p a");
         for (Element link : links) 
         {
         
    	  
        	           
        	 if (link.text().matches("^[\\p{javaUpperCase}].*") == true) 

        	 {                            
        		 list.add(new NamedLink(link.absUrl("href"), link.text()));
        	 }
         }
     
        
         return list;
		 
	 }
	 
	 
	 
	 /**
		 * This  method finds the title of the page 
		 *
		 * @param url url of one page
		 * @return  object that contains the title and same url
		 *
		 * @author Zakaria BOUHAMIDI <zakariabouhamidi@hotmail.com>
	 * @throws Exception 
		 */
	 
	 public static NamedLink getNamedLink(String url) throws Exception
	 {
		 String title="";
		 Document doc = null;
		 NamedLink result;
		 
		try {
				doc = Jsoup.connect(url).get();
		    } 
		catch (IOException e) 
		    {
			 	// TODO Auto-generated catch block
				e.printStackTrace();
		    }
		
		try{
			Elements es = doc.select("#firstHeading");
			if(es!=null)
				title = es.text();
			
			result=new NamedLink(url,title);
		}catch(Exception ex){
			throw new Exception("error in url " + url);
		}
		
		 return result;
		 
	 }
	

}







